package com.jss.jssmybatisplus.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.jss.jssmybatisplus.dto.ResultResponse;
import com.jss.jssmybatisplus.dto.req.QueryByPageReqDTO;
import com.jss.jssmybatisplus.dto.resp.OrderIndoRespDTO;
import com.jss.jssmybatisplus.entity.ProductEntity;
import com.jss.jssmybatisplus.entity.UserInfoEntity;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.jss.jssmybatisplus.dao.OrderDao;
import com.jss.jssmybatisplus.entity.OrderEntity;
import com.jss.jssmybatisplus.service.OrderService;

import javax.annotation.Resource;


/**
 * @author Shuaishuai.Ji
 */
@Service("orderService")
public class OrderServiceImpl extends ServiceImpl<OrderDao, OrderEntity> implements OrderService {

    @Resource
    private  OrderDao orderDao;


    @Override
    public ResultResponse<OrderIndoRespDTO> getOrderInfo(String orderNumber) {
        OrderIndoRespDTO orderIndoRespDTO = orderDao.selectJoinOne(OrderIndoRespDTO.class,
                                                                  new MPJLambdaWrapper<OrderEntity>()
            .selectAll(OrderEntity.class)
            .select(ProductEntity::getProducPrice,ProductEntity::getProductName)
            .selectAs(UserInfoEntity::getUserName,OrderIndoRespDTO::getUserName)
            .selectAs(ProductEntity::getProducPrice,OrderIndoRespDTO::getProductPrice)
            .leftJoin(ProductEntity.class,ProductEntity::getProductId,OrderEntity:: getProductId)
            .leftJoin(UserInfoEntity.class,UserInfoEntity::getUserId,OrderEntity::getUserId)
            .eq(OrderEntity::getOrderNumber,orderNumber));

//        OrderEntity orderEntity = orderDao.selectOne(
//            new LambdaQueryWrapper<OrderEntity>().eq(OrderEntity::getOrderNumber, orderNumber));
//        System.out.println(orderEntity);
        return ResultResponse.succResult(orderIndoRespDTO);
    }

    @Override
    public ResultResponse<IPage<OrderIndoRespDTO>> queryByPage(QueryByPageReqDTO queryByPageReqDTO) {
        int pageNum = queryByPageReqDTO.getPageNum();
        int pageSize = queryByPageReqDTO.getPageSize();
        MPJLambdaWrapper<OrderEntity> orderEntityMPJLambdaWrapper = new MPJLambdaWrapper<OrderEntity>().selectAll(
            OrderEntity.class).select(ProductEntity::getProducPrice, ProductEntity::getProductName).selectAs(
            UserInfoEntity::getUserName, OrderIndoRespDTO::getUserName).selectAs(ProductEntity::getProducPrice,
                                                                                 OrderIndoRespDTO::getProductPrice).leftJoin(
            ProductEntity.class, ProductEntity::getProductId, OrderEntity::getProductId).leftJoin(UserInfoEntity.class,
                                                                                                  UserInfoEntity::getUserId,
                                                                                                  OrderEntity::getUserId);
        IPage<OrderIndoRespDTO> orderIndoRespDTOIPage = orderDao.selectJoinPage(new Page<>(pageNum, pageSize),
                                                                                OrderIndoRespDTO.class,
                                                                                orderEntityMPJLambdaWrapper);

        return ResultResponse.succResult(orderIndoRespDTOIPage);
    }

}